#!/usr/bin/env python
import ez_setup
ez_setup.use_setuptools()

import sys, os, os.path

from setuptools import setup, find_packages
from setuptools.extension import Extension

BOOST_HOME = None
BOOST_PYTHON_MT = False
PYTHON_HOME = None
PAGESPEED_HOME = None

# load defaults from config file
try:
    from buildconf import *
except ImportError:
    pass

BOOST_HOME = os.environ.get('BOOST_HOME', BOOST_HOME)
BOOST_PYTHON_MT = os.environ.get('BOOST_PYTHON_MT', BOOST_PYTHON_MT)
PYTHON_HOME = os.environ.get('PYTHON_HOME', PYTHON_HOME)
PAGESPEED_HOME = os.environ.get('PAGESPEED_HOME', PAGESPEED_HOME)

if not PAGESPEED_HOME:
    print "ERROR: you should set PAGESPEED_HOME to the Google mod_pagespeed folder, or download and build it first. <http://code.google.com/p/modpagespeed/>"
    sys.exit()

source_files = ["Exception.cpp", "Filter.cpp", "Node.cpp", "Parser.cpp", "Module.cpp"]

macros = [
    ("BOOST_PYTHON_STATIC_LIB", None),
]

include_dirs = [
    os.path.join(PAGESPEED_HOME, 'src'),
    os.path.join(PAGESPEED_HOME, 'src/third_party/chromium/src'),
    os.path.join(PAGESPEED_HOME, 'src/third_party/protobuf2/src/src'),
]
library_dirs = []
libraries = ["base", "googleurl", "instaweb_htmlparse_core", "instaweb_util_core"]
extra_compile_args = []
extra_link_args = []

if BOOST_HOME:
    include_dirs += [BOOST_HOME]
    library_dirs += [
        os.path.join(BOOST_HOME, 'stage/lib'),
    ]

if os.name == "nt":
    include_dirs += [
        os.path.join(PYTHON_HOME, 'include'),
    ]
    library_dirs += [
        os.path.join(PYTHON_HOME, 'libs'),
        os.path.join(PAGESPEED_HOME, 'src/net/instaweb/Release/lib'),
    ]
    libraries += ["dbghelp", "advapi32", "user32", "ole32"]

    extra_compile_args += ["/O2", "/GL", "/MT", "/EHsc", "/Gy", "/Zi"]
    extra_link_args += ["/DLL", "/OPT:REF", "/OPT:ICF", "/MACHINE:X86"]

elif os.name == "posix" and sys.platform == "linux2":
    if PYTHON_HOME:
        major, minor, _, _, _ = sys.version_info

        include_dirs += [os.path.join(PYTHON_HOME, 'include')]
        library_dirs += [os.path.join(PYTHON_HOME, 'lib/python%d.%d' % (major, minor))]
        libraries += ["boost_python-mt" if BOOST_PYTHON_MT else "boost_python"]

    library_dirs += [
        os.path.join(PAGESPEED_HOME, 'src/out/Release/obj.target/base/'),
        os.path.join(PAGESPEED_HOME, 'src/out/Release/obj.target/net/instaweb/'),
        os.path.join(PAGESPEED_HOME, 'src/out/Release/obj.target/build/temp_gyp/'),
    ]

    libraries += [
        "icuuc",
        "boost_python-mt" if BOOST_PYTHON_MT else "boost_python",
        "base",
        "rt",
        "googleurl",
    ]
else:
    print "ERROR: unsupported OS (%s) and platform (%s)" % (os.name, sys.platform)

pyinstaweb = Extension(
    name = "_pyinstaweb",
    sources = [os.path.join("src", file) for file in source_files],
    define_macros = macros,
    include_dirs = include_dirs,
    library_dirs = library_dirs,
    libraries = libraries,
    extra_compile_args = extra_compile_args,
    extra_link_args = extra_link_args,
)

setup(
    name = 'pyinstaweb',
    version = '0.3',
    packages = find_packages(exclude=['ez_setup', 'tests']),
    author = 'Flier Lu',
    author_email = 'flier.lu@gmail.com',
    description = 'Python binding for instaweb to parse malformed HTML with Beautiful Soup',
    long_description = open('README.txt').read(),
    license = 'Apache License 2.0',
    keywords = 'Python HTML BeautifulSoup Parsing Web Developer',
    url = 'http://code.google.com/p/pyinstaweb/',
    download_url = 'http://code.google.com/p/pyinstaweb/downloads/list',
    install_requires = [
        'BeautifulSoup >= 3.1.0',
    ],
    py_modules=['pyinstaweb'],
    ext_modules=[pyinstaweb],
    classifiers = [
        'Development Status :: 4 - Beta',
        'Intended Audience :: Developers',
        'Intended Audience :: System Administrators',
        'License :: OSI Approved :: Apache Software License',
        'Natural Language :: English',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Topic :: Internet',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Software Development',
    ]
)
